package org.fage.semples.web;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.fage.semples.model.CommonMessageResultVO;
import org.fage.semples.model.ExamUser;
import org.fage.semples.model.PagerModel;
import org.fage.semples.service.ExamUserHiberanteService;
import org.fage.semples.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
public class UserController {

	
	@Autowired
	private ExamUserHiberanteService examUserHiberanteService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
//	@Autowired
//	private MessageSource messageSource;
	
	//查询
	@RequestMapping("/users")
	public String users(Model model){
		
		PagerModel<ExamUser> page = examUserHiberanteService.findAllByPage();
		model.addAttribute("url", "users");
		model.addAttribute("page_jsp", "1");
		model.addAttribute("page", page);
		
//		List<ExamUser> list = examUserHiberanteService.findAllExamUsers();
//		model.addAttribute("edit",messageSource.getMessage("page.index.edit",null,LocaleContextHolder.getLocale()));
//		model.addAttribute("userList", list);
		return "user/userList";
	}
	
	//批量添加学生
	@ResponseBody
	@RequestMapping("/uploadFile")
	public CommonMessageResultVO uploadFile(@RequestParam(value = "uploadFile") MultipartFile[] uploadFile, HttpSession session, Model model){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		MultipartFile uploadSimpleFile = uploadFile[0];
		String fileName = uploadSimpleFile.getOriginalFilename();
		//获得文件后缀名
		String postfix = fileName.substring(fileName.lastIndexOf("."));
		Map<String, String> messageMap = new HashMap<String, String>();
		
		try{
			if(ExcelUtils.OFFICE_EXCEL_2007_POSTFIX.equals(postfix) || ExcelUtils.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
				examUserHiberanteService.createExamUserList(uploadSimpleFile.getInputStream(), postfix);
				resultVO.setSuccess(true);
				
			}else{
				
				messageMap.put("message", "文件格式不正确");
				resultVO.setMessageMap(messageMap);
			}
		}catch(Exception e){
			System.out.println("55555");
			e.printStackTrace();
			messageMap.put("message", "文件格式不正确");
			resultVO.setMessageMap(messageMap);
		}
		
		return resultVO;
	}
	
	//新增
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("examUser", new ExamUser());
		return "user/add";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes,@Validated ExamUser examUser, BindingResult result){

		if(result.hasErrors()){
			return "user/add";
		}
		try{
			examUserHiberanteService.createExamUser(examUser);
			redirectAttributes.addFlashAttribute("message", "添加成功");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/users";
	}
	
	//修改
	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.GET)
	public String toEdit(Model model,@PathVariable String userId) throws Exception{
		ExamUser examUser = examUserHiberanteService.getExamUserById(userId);
		model.addAttribute("examUser", examUser);
		return "user/edit";
	}
	
	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes,ExamUser examUser,@PathVariable String userId){
		try{
			
			examUserHiberanteService.editExamUser(examUser);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/users";
	}
	
	//删除
	@RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable String userId){
		try {
			examUserHiberanteService.deleteExamUser(userId);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/users";
	}
	
	
	//语言切换
	@RequestMapping(value = "/changeLocale")
	public String changeLocale(HttpServletRequest request,HttpServletResponse response, String localeType){
		if("en_US".equals(localeType)){
			localeResolver.setLocale(request,response,Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request,response,Locale.CHINA);
		}
		return "redirect:users";
	}
}
